/**
 * created by 小卷毛, 2020-06-16 016
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.zhiyou.aifeng.common_ui.utils

import android.app.Activity
import android.content.Context
import android.text.InputFilter
import android.view.KeyEvent
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.flyco.dialog.widget.ActionSheetDialog
import com.maning.mndialoglibrary.MProgressDialog
import com.maning.mndialoglibrary.config.MDialogConfig
import com.weigan.loopview.LoopView
import com.zhiyou.aifeng.common.base.*
import com.zhiyou.aifeng.common.helper.DecimalNumberFilter
import com.zhiyou.aifeng.common.helper.textString
import com.zhiyou.aifeng.common_ui.R
import com.zhiyou.aifeng.common_ui.base.BaseDialog
import com.zhiyou.aifeng.common_ui.base.BottomDialog
import org.jetbrains.anko.toast

object DialogHelper {

    fun showDialog(context: Context, hint: String = "") {
        dismissDialog()

        MProgressDialog.showProgress(
            context,
            hint,
            MDialogConfig.Builder()
                .isCancelable(true)
                .isCanceledOnTouchOutside(false)
                .build()
        )
    }

    fun dismissDialog() {
        if (MProgressDialog.isShowing())
            MProgressDialog.dismissProgress()
    }

}

fun Activity.showLoadingDialog(hint: String = "加载中...") =
    DialogHelper.showDialog(this, hint)

@Suppress("unused")
fun Activity.cancelLoadingDialog() = DialogHelper.dismissDialog()

fun Context.showActionDialog(
    vararg items: String,
    listener: (Int, String) -> Unit
) {
    ActionSheetDialog(this, items, null).apply {
        isTitleShow(false)
        lvBgColor(getColorEx(R.color.white))
        itemTextColor(getColorEx(R.color.black))
        itemHeight(45f)
        itemTextSize(15f)
        dividerHeight(0.5f)
        dividerColor(getColorEx(R.color.divider))
        cancelText(getColorEx(R.color.light))
        cancelTextSize(15f)
        layoutAnimation(null)
        show()

        setOnOperItemClickL { _, _, index, _ ->
            dismiss()
            listener.invoke(index, items[index])
        }
    }
}

fun Context.showRealDialog(
    onClick: () -> Unit = {}
): BaseDialog {
    val dialog = object : BaseDialog(this) {

        override fun onCreateView(): View {
            widthScale(0.0f)
            return inflate(R.layout.dialog_real_hint)
        }

        override fun setUiBeforShow() {
            createView
                .find<Button>(R.id.bt_real)
                .oneClick {
                    onClick.invoke()
                }
        }
    }

    dialog.setOnKeyListener { _, keyCode, _ ->
        return@setOnKeyListener keyCode == KeyEvent.KEYCODE_BACK
    }
    dialog.setCanceledOnTouchOutside(false)
    dialog.show()

    return dialog
}

fun Context.showInputDialog(
    hint: String = "",
    onResult: (String) -> Unit = {}
) {
    val dialog = object : BaseDialog(this, true) {

        override fun onCreateView(): View {
            widthScale(0.0f)
            return inflate(R.layout.dialog_order_input)
        }

        override fun setUiBeforShow() {
            val mInput = createView.find<EditText>(R.id.dialog_input)
            mInput.apply {
                filters = arrayOf<InputFilter>(DecimalNumberFilter())
                setText(hint)
                setSelection(text.length)
            }

            createView.find<ImageView>(R.id.dialog_close).oneClick { dismiss() }
            createView.find<Button>(R.id.dialog_sure).oneClick {
                if (mInput.text.isEmpty()) {
                    toast("请输入作业的亩数")
                    return@oneClick
                }

                dismiss()
                onResult.invoke(mInput.textString())
            }
        }
    }

    dialog.setCanceledOnTouchOutside(true)
    dialog.show()
}

fun Context.showOneHintDialog(
    title: String = "温馨提示",
    hint: String = "",
    sure: String = "我知道了",
    isForced: Boolean = false,
    onResult: () -> Unit = {}
) {
    val dialog = object : BaseDialog(this) {

        override fun onCreateView(): View {
            widthScale(0.7f)
            val view = inflate<View>(R.layout.dialog_remind_hint_one)

            val tvTitle = view.findViewById<TextView>(R.id.dialog_title)
            val tvHint = view.findViewById<TextView>(R.id.dialog_hint)
            val btOk = view.findViewById<TextView>(R.id.dialog_ok)

            tvTitle.text = title
            tvHint.text = hint
            btOk.text = sure

            btOk.oneClick {
                dismiss()
                onResult.invoke()
            }

            return view
        }
    }

    dialog.setOnKeyListener { _, keyCode, _ ->
        if (keyCode == KeyEvent.KEYCODE_BACK) return@setOnKeyListener isForced
        else return@setOnKeyListener false
    }
    dialog.setCanceledOnTouchOutside(!isForced)
    dialog.show()
}

fun Context.showHintDialog(
    title: String = "温馨提示",
    hint: String = "",
    cancel: String = "取消",
    sure: String = "确定",
    isForced: Boolean = false,
    onCancel: () -> Unit = {},
    onSure: () -> Unit = {}
): BaseDialog {
    val dialog = object : BaseDialog(this) {

        override fun onCreateView(): View {
            widthScale(0.7f)
            val view = inflate<View>(R.layout.dialog_remind_hint)

            val tvTitle = view.findViewById<TextView>(R.id.dialog_title)
            val tvHint = view.findViewById<TextView>(R.id.dialog_hint)
            val btCancel = view.findViewById<TextView>(R.id.dialog_cancel)
            val btSure = view.findViewById<TextView>(R.id.dialog_sure)

            tvTitle.text = title
            tvHint.text = hint
            btCancel.text = cancel
            btSure.text = sure

            btCancel.oneClick {
                dismiss()
                onCancel.invoke()
            }

            btSure.oneClick {
                dismiss()
                onSure.invoke()
            }

            return view
        }
    }

    dialog.setOnKeyListener { _, keyCode, _ ->
        if (keyCode == KeyEvent.KEYCODE_BACK) return@setOnKeyListener isForced
        else return@setOnKeyListener false
    }
    dialog.setCanceledOnTouchOutside(!isForced)
    dialog.show()

    return dialog
}

/**
 * 滚轮效果单个item选择
 */
fun Context.showListDialog(
    title: String = "请选择",
    index: Int = -1,
    items: List<String>,
    listener: (Int, String) -> Unit = { _, _ -> }
) {

    val dialog = object : BottomDialog(this) {

        private lateinit var loopView: LoopView

        override fun onCreateView(): View {
            val view = inflate<View>(R.layout.dialog_select_more)

            val tvTitle = view.findViewById<TextView>(R.id.tv_dialog_select_title)
            val tvCancel = view.findViewById<TextView>(R.id.tv_dialog_select_cancle)
            val tvOk = view.findViewById<TextView>(R.id.tv_dialog_select_ok)

            loopView = view.find(R.id.lv_dialog_select_year)
            view.find<LoopView>(R.id.lv_dialog_select_month).gone()
            view.find<LoopView>(R.id.lv_dialog_select_day).gone()
            view.find<LoopView>(R.id.lv_dialog_select_hour).gone()
            view.find<LoopView>(R.id.lv_dialog_select_minute).gone()

            tvTitle.text = title
            loopView.setTextSize(15f)
            loopView.setDividerColor(getColorEx(R.color.divider))
            loopView.setNotLoop()

            tvCancel.oneClick { dismiss() }

            tvOk.oneClick {
                dismiss()
                listener.invoke(loopView.selectedItem, items[loopView.selectedItem])
            }

            return view
        }

        override fun setUiBeforShow() {
            loopView.setItems(items)
            loopView.setInitPosition(if (index < 0) 0 else index)
        }

    }

    dialog.show()
}